package com.yudean.esb.service.impl.mds;

import com.yudean.esb.dao.mds.MdsIntIndexDao;
import com.yudean.esb.domain.mds.MdsIntIndex;
import com.yudean.esb.service.MdsIntIndexService;
import com.yudean.esb.security.SysAppUserService;
import com.yudean.esb.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebService(endpointInterface = "com.yudean.esb.service.MdsIntIndexService", serviceName = "MdsIntIndexServiceService",targetNamespace = "http://service.esb.yudean.com/")
@Service("mdsIntIndexService")
@Scope("prototype")
@Slf4j
public class MdsIntIndexServiceImpl implements MdsIntIndexService {
    @Resource
    private MdsIntIndexDao mdsIntIndexDao;

    @Resource
    private SysAppUserService sysAppUserService;

    @Override
    public String getMdsIntIndexByPeriodDate(String periodDate, String compCode, String userName, String passwd) {
        log.info("getMdsIntIndexByPeriodDate:reuqest-{periodDate:{},compCode:{},userName:{}}", periodDate, compCode, userName);
        if (!hasPermission(compCode, userName, passwd)) {
            return noPermission();
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date datePeriodDate = sdf.parse(periodDate);
            List<MdsIntIndex> mdsIntIndexList =
                    mdsIntIndexDao.getMdsIntIndexByPeriodDate(datePeriodDate, compCode);
            if (mdsIntIndexList == null || mdsIntIndexList.size() < 1) {
                return noData();
            }
            return wrapMdsIntIndex2XML(mdsIntIndexList);
        } catch (Exception ex) {
            log.error("getMdsIntIndexByPeriodDate:error-{}", ex.getMessage());
            return errorMsg("getMdsIntIndexByPeriodDate", ex.getMessage());
        }
    }

    @Override
    public String getMdsIntIndexByPeriod(String periodStartDate, String periodEndDate, String periodType, String compCode, String userName, String passwd) {
        log.info("getMdsIntIndexByPeriod:reuqest-{periodStartDate:{}，periodEndDate:{},compCode:{},userName:{}}", periodStartDate, periodEndDate, compCode, userName);
        if (!hasPermission(compCode, userName, passwd)) {
            return noPermission();
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date datePeriodStartDate = sdf.parse(periodStartDate);
            Date datePeriodEndDate = sdf.parse(periodEndDate);
            List<MdsIntIndex> mdsIntIndexList =
                    mdsIntIndexDao.getMdsIntIndexByPeriod(datePeriodStartDate, datePeriodEndDate, periodType, compCode);
            if (mdsIntIndexList == null || mdsIntIndexList.size() < 1) {
                return noData();
            }
            return wrapMdsIntIndex2XML(mdsIntIndexList);
        } catch (Exception ex) {
            log.error("getMdsIntIndexByPeriod:error-{}", ex.getMessage());
            return errorMsg("getMdsIntIndexByPeriod", ex.getMessage());
        }
    }

    private String wrapMdsIntIndex2XML(List<MdsIntIndex> mdsIntIndexList) {
        Document root = null;
        root = DocumentHelper.createDocument();
        Element element = root.addElement("service");
        element.addElement("name").setText("财务指标数据");
        element.addElement("type").setText("发布服务");
        Element datalist = element.addElement("datalist");
        for (int i = 0; i < mdsIntIndexList.size(); i++) {
            MdsIntIndex mdsIntIndex = mdsIntIndexList.get(i);
            Element data = datalist.addElement("data");
            data.addElement("mdsIntIndexId").setText(StringUtil.convertToString(mdsIntIndex.getMdsIntIndexId()));
            data.addElement("indexCode").setText(StringUtil.convertToString(mdsIntIndex.getIndexCode()));
            data.addElement("indexName").setText(StringUtil.convertToString(mdsIntIndex.getIndexName()));
            data.addElement("compCode").setText(StringUtil.convertToString(mdsIntIndex.getCompCode()));
            data.addElement("periodType").setText(StringUtil.convertToString(mdsIntIndex.getPeriodType()));
            data.addElement("periodYear").setText(StringUtil.convertToString(mdsIntIndex.getPeriodYear()));
            data.addElement("periodQuarter").setText(StringUtil.convertToString(mdsIntIndex.getPeriodQuarter()));
            data.addElement("periodName").setText(StringUtil.convertToString(mdsIntIndex.getPeriodName()));
            data.addElement("periodDate").setText(StringUtil.date2ShortString(mdsIntIndex.getPeriodDate()));
            data.addElement("periodValue").setText(StringUtil.convertToString(mdsIntIndex.getPeriodValue()));
            data.addElement("endValue").setText(StringUtil.convertToString(mdsIntIndex.getEndValue()));
            data.addElement("createdBy").setText(StringUtil.convertToString(mdsIntIndex.getCreatedBy()));
            data.addElement("creationDate").setText(StringUtil.date2String(mdsIntIndex.getCreationDate()));
            data.addElement("lastUpdateDate").setText(StringUtil.date2String(mdsIntIndex.getLastUpdateDate()));
            data.addElement("lastUpdatedBy").setText(StringUtil.convertToString(mdsIntIndex.getLastUpdatedBy()));
            data.addElement("lastUpdateLogin").setText(StringUtil.convertToString(mdsIntIndex.getLastUpdateLogin()));

            data.addElement("context").setText(StringUtil.convertToString(mdsIntIndex.getContext()));
            data.addElement("attribute1").setText(StringUtil.convertToString(mdsIntIndex.getAttribute1()));
            data.addElement("attribute2").setText(StringUtil.convertToString(mdsIntIndex.getAttribute2()));
            data.addElement("attribute3").setText(StringUtil.convertToString(mdsIntIndex.getAttribute3()));
            data.addElement("attribute4").setText(StringUtil.convertToString(mdsIntIndex.getAttribute4()));
            data.addElement("attribute5").setText(StringUtil.convertToString(mdsIntIndex.getAttribute5()));
            data.addElement("attribute6").setText(StringUtil.convertToString(mdsIntIndex.getAttribute6()));
            data.addElement("attribute7").setText(StringUtil.convertToString(mdsIntIndex.getAttribute7()));
            data.addElement("attribute8").setText(StringUtil.convertToString(mdsIntIndex.getAttribute8()));
            data.addElement("attribute9").setText(StringUtil.convertToString(mdsIntIndex.getAttribute9()));
            data.addElement("attribute10").setText(StringUtil.convertToString(mdsIntIndex.getAttribute10()));
            data.addElement("attribute11").setText(StringUtil.convertToString(mdsIntIndex.getAttribute11()));
            data.addElement("attribute12").setText(StringUtil.convertToString(mdsIntIndex.getAttribute12()));
            data.addElement("attribute13").setText(StringUtil.convertToString(mdsIntIndex.getAttribute13()));
            data.addElement("attribute14").setText(StringUtil.convertToString(mdsIntIndex.getAttribute14()));
            data.addElement("attribute15").setText(StringUtil.convertToString(mdsIntIndex.getAttribute15()));

        }
        return root.asXML();
    }


    /**
     * 权限认证失败返回的信息
     *
     * @return
     */
    private static String noPermission() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>ERP财务指标数据</name><type>发布服务</type><datalist><!--权限认证失败--></datalist></service>";
    }

    /**
     * 验证是否拥有访问的权限
     *
     * @param compCode
     * @param userName
     * @param passwd
     * @return
     */
    private boolean hasPermission(String compCode, String userName,
                                  String passwd) {
        return sysAppUserService.hasPermit(compCode, userName, passwd);
    }

    /**
     * 返回空数据的时候的信息
     *
     * @return
     */
    private static String noData() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>ERP财务指标数据</name><type>发布服务</type><datalist><!--查无数据--></datalist></service>";
    }

    /**
     * 返回查询出错信息
     *
     * @param msg
     * @return
     */
    private String errorMsg(String serviceName, String msg) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>" + serviceName + "</name><type>发布服务</type><datalist><!--error Message:" + msg + "--></datalist></service>";
    }
}
